<?php

namespace lib;
use auth;
class PluginScanner
{
    public static function getPluginInfo($pluginDir)
    {
        $plugins = [];
        
        $directoryIterator = new \RecursiveDirectoryIterator($pluginDir, \RecursiveDirectoryIterator::SKIP_DOTS);
        $iterator = new \RecursiveIteratorIterator($directoryIterator);
    
        foreach ($iterator as $file) {
            if ($file->isFile() && $file->getFilename() === 'config.json') {
                $contents = file_get_contents($file->getRealPath());
                $pluginInfo = json_decode($contents, true);
                if ($pluginInfo) {
                    $plugins[] = $pluginInfo;
                }
            }
        }
        return $plugins;
    }
    public static function key($priKey,$key,$file){
        $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
                wordwrap($priKey, 64, "\n", true) .
                "\n-----END RSA PRIVATE KEY-----";
        define('privateKey',$res);
        define('pluginpwd',md5($key));
        define("authfile_path",SYSTEM_ROOT.'auth/'.$file);
        return md5($key);
    }
    
    public static function authxy($priKey,$key,$file,$send){
        global  $conf;
        self::key($priKey,$key,$file);
        if(!$send)return true;
        $auth = false;
        if (file_exists(authfile_path)){
            $token = file_get_contents(authfile_path);
            $auth  = auth::mactokenxy($token,pluginpwd,privateKey);
        }
        if(!$auth){
            include pluginstem.'temp.php';
            exit;
        }
        return $auth;
    }
    
    
}
?>